package com.mobileares.midp.widgets.client.textbox2.widget;

import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.*;
import com.mobileares.midp.widgets.client.button.CButton;
import com.mobileares.midp.widgets.client.list.CheckBoxListItem;
import com.mobileares.midp.widgets.client.list.ListItem;
import com.mobileares.midp.widgets.client.list.ListWidget;
import com.mobileares.midp.widgets.client.list.MultiListWidget;
import com.mobileares.midp.widgets.client.utils.CodeModel;
import com.mobileares.midp.widgets.client.utils.ICodeProvider;
import com.mobileares.midp.widgets.client.utils.IWidgetProvider;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-4-9
 * Time: 17:18:28
 * To change this template use File | Settings | File Templates.
 */
public class MultiListBox extends Composite {
    private HorizontalPanel container;
    private MultiListWidget codes;
    private ListWidget values;
    private CButton add;
    private CButton remove;
    private TextBox filter;

    private boolean isFilter;
    private List codeValues = new ArrayList();
    private static final String _TIP_WORD = "输入关键字";


    private IWidgetProvider codeProvider = new IWidgetProvider<CodeModel>() {
        public Object getUserObject(CodeModel ob) {
            return ob.getCode();
        }

        public Widget getWidget(CodeModel ob) {
            return new Label(ob.getText());
        }
    };

    public MultiListBox() {
        this(false);
    }

    public MultiListBox(boolean isFilter) {
        container = new HorizontalPanel();
        container.setSpacing(1);
        codes = new MultiListWidget();
        codes.setProvider(codeProvider);
        values = new ListWidget();
        values.setProvider(codeProvider);
        add = new CButton("添加");
        remove = new CButton("删除");
        codes.setSize("150px", "180px");
        values.setSize("150px", "180px");

        VerticalPanel buttons = new VerticalPanel();
        buttons.add(add);
        buttons.add(new HTML("&nbsp;"));
        buttons.add(remove);

        container.add(values);
        container.add(buttons);

        this.isFilter = isFilter;
        if (this.isFilter) {
            VerticalPanel vp = new VerticalPanel();
            vp.setSize("150", "180");
            vp.setSpacing(1);
            filter = new TextBox();
            filter.setValue(_TIP_WORD);
            DOM.setStyleAttribute(filter.getElement(), "color", "#BABABA");
            DOM.setStyleAttribute(filter.getElement(), "fontSize", "12px");

            filter.setSize("148px", "18px");
            codes.setSize("150px", "160px");
            vp.add(filter);
            vp.add(codes);
            container.add(vp);
            initFilter();
        } else {
            container.add(codes);
        }
        initEvent();
        initWidget(container);
    }

    public void setCodeProvider(IWidgetProvider codeProvider) {
        this.codeProvider = codeProvider;
        values.setProvider(codeProvider);
        codes.setProvider(codeProvider);
    }

    public void addItem(final String text, String value) {
        CheckBoxListItem item = new CheckBoxListItem(new CodeModel(text, value));
        codes.addItem(item);
        codeValues.add(item);
    }

    public void removeAll() {
        values.removeAll();
        codes.removeAll();
        codeValues.clear();
    }

    public ListWidget getCodeWidget() {
        return codes;
    }

    public ListWidget getValueWidgets() {
        return values;
    }

    private void initEvent() {
        add.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                List userObjects = (List) codes.getSelected();
                for (Iterator it = userObjects.iterator(); it.hasNext();) {
                    CheckBoxListItem temp = (CheckBoxListItem) it.next();
                    if (!isExistItem(temp.getUserObject())) {
                        values.addItem(temp.getUserObject());
                    }
                }
            }
        });
        remove.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                ListItem item = (ListItem) values.getSelected();
//                for (Iterator it = userObjects.iterator(); it.hasNext();) {
//                    FishListWidget.ListItem temp = (FishListWidget.ListItem) it.next();
//                    values.removeItem(temp);
//                }
                if (item != null) {
                    item.removeFromParent();
                }
            }
        });
    }

    private boolean isExistItem(Object userObject) {
        for (Iterator it = values.getIterator(); it.hasNext();) {
            ListItem temp = (ListItem) it.next();
            if (temp.getUserObject().equals(userObject))
                return true;
        }
        return false;
    }

    private void initFilter() {
        filter.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                String key = filter.getValue();
                if (_TIP_WORD.equals(key))
                    filter.setValue("");
            }
        });
        filter.addBlurHandler(new BlurHandler() {
            public void onBlur(BlurEvent event) {
                String key = filter.getValue();
                if (key == null || key.trim().equals(""))
                    filter.setValue(_TIP_WORD);
            }
        });
        filter.addChangeHandler(new ChangeHandler() {
            public void onChange(ChangeEvent event) {
                codes.removeAll();
                String key = filter.getValue();
                for (Iterator it = codeValues.iterator(); it.hasNext();) {
                    ListItem item = (ListItem) it.next();
                    if (key == null || key.trim().equals("")) {
                        codes.addItem(item);
                    } else if (codeProvider.getUserObject(item.getUserObject()).toString().indexOf(key) != -1 ||
                            codeProvider.getUserObject(item.getUserObject()).toString().indexOf(key) != -1) {
                        codes.addItem(item);
                    }
                }
            }
        });
    }

    private class TextBoxEvents extends HandlesAllKeyEvents implements
            ValueChangeHandler<String>, ClickHandler {

        public void onKeyUp(KeyUpEvent event) {
            fireFilter();
        }

        public void onKeyDown(KeyDownEvent event) {
            fireFilter();
        }

        public void onKeyPress(KeyPressEvent event) {
            //To change body of implemented methods use File | Settings | File Templates.
        }

        public void onValueChange(ValueChangeEvent<String> stringValueChangeEvent) {
            fireFilter();
        }

        public void onClick(ClickEvent event) {
            fireFilter();
        }
    }

    private void fireFilter() {


    }

}
